package cn.donting.jme.expansion.inspect;

import cn.donting.jme.EditorContent;
import cn.donting.jme.event.spatial.SpatialTransformChangeEvent;
import cn.donting.jme.expansion.attribute.Attribute;
import cn.donting.jme.expansion.attribute.TitlePaneAttributeGroup;
import cn.donting.jme.expansion.builder.spatial.TransformAttributeBuilder;
import cn.donting.jme.handle.EvenHandle;
import cn.donting.jme.inspect.Inspect;
import cn.donting.jme.ioc.Bean;
import cn.donting.jme.ioc.Sort;
import com.formdev.flatlaf.extras.FlatSVGIcon;
import com.google.common.eventbus.Subscribe;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;

import java.util.ArrayList;
import java.util.List;

@Bean
@Sort(1)
@EvenHandle
public class TransformInspectBuilder implements InspectBuilder<Spatial> {


    private TitlePaneAttributeGroup transformAttributeGroup;
    private Spatial spatial;

    public TransformInspectBuilder() {
    }

    @Override
    public boolean isHandle(Object o) {
        return o instanceof Spatial;
    }

    @Override
    public void builder(Inspect inspect, Spatial spatial) {
        TitlePaneAttributeGroup titlePaneAttributeGroup = new TitlePaneAttributeGroup("transform");
        this.spatial = spatial;
        this.transformAttributeGroup = titlePaneAttributeGroup;
        titlePaneAttributeGroup.setTitleIcon(new FlatSVGIcon("icon/transform.svg", 16, 16));
        List<TransformAttributeBuilder> transformAttributeBuilders = EditorContent.getIocContainer().getBeansByTypes(TransformAttributeBuilder.class);
        for (TransformAttributeBuilder transformAttributeBuilder : transformAttributeBuilders) {
            transformAttributeBuilder.setSpatial(spatial);
            List<Attribute> attributes = new ArrayList<>();
            transformAttributeBuilder.attribute(attributes, spatial.getLocalTransform());
            for (Attribute attribute : attributes) {
                titlePaneAttributeGroup.addAttribute(attribute);
            }
        }
        titlePaneAttributeGroup.setExpand(true);
        inspect.addAttributeGroup(titlePaneAttributeGroup);
    }

    @Subscribe
    private void spatialTransformChangeEvent(SpatialTransformChangeEvent event) {
        if (this.transformAttributeGroup != null && spatial == event.getSpatial()) {
            List<Attribute> attributes = transformAttributeGroup.getAttributes();
            for (Attribute attribute : attributes) {
                if (TransformAttributeBuilder.TRANSLATION_ATTRIBUTE_KEY.equals(attribute.getKey())) {
                    attribute.setValue(event.getSpatial().getLocalTranslation());
                }
                if (TransformAttributeBuilder.ROTATION_ATTRIBUTE_KEY.equals(attribute.getKey())) {
                    Quaternion rotation = spatial.getLocalRotation();
                    float[] angles = rotation.toAngles(new float[3]);
                    Vector3f r = new Vector3f(angles[0] / FastMath.PI * 180, angles[1] / FastMath.PI * 180, angles[2] / FastMath.PI * 180);
                    attribute.setValue(r);
                }
                if (TransformAttributeBuilder.SCALE_ATTRIBUTE_KEY.equals(attribute.getKey())) {
                    attribute.setValue(spatial.getLocalScale());
                }
            }
        }

    }
}
